package com.net.framework.help.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;

import com.net.framework.help.R;


public class BgAnim {
    private Context context;
    private View view;
    /**
     * 下标
     */
    private int index;

    private int[] mAnimations = {R.anim.login_bg_anim_toleft, R.anim.login_bg_anim_toright};

    //    private TranslateAnimation anim;
    public BgAnim(Context context) {
        this.context = context;
    }
//    public Animation getAnim(final View v) {
//        anim = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -2f,
//                Animation.RELATIVE_TO_PARENT, -0.7f,
//                Animation.RELATIVE_TO_PARENT, 0f, Animation.RELATIVE_TO_PARENT,
//                0f);
//        anim.setDuration(20000);
//        anim.setFillAfter(true);
//        anim.setRepeatMode(Animation.REVERSE);
//        anim.setRepeatCount(Animation.INFINITE);
//        anim.setAnimationListener(new Animation.AnimationListener() {
//
//            public void onAnimationStart(Animation animation) {
//            }
//
//            public void onAnimationRepeat(Animation animation) {
//            }
//
//            public void onAnimationEnd(Animation animation) {
//                // TODO Auto-generated method stub
//                v.startAnimation(anim);
//            }
//        });
//        return anim;
//    }

    public void getAnim(View v) {
        this.view = v;
        handler.sendEmptyMessage(index++);
    }


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            //补间动画
            AnimationSet anim = (AnimationSet) AnimationUtils.loadAnimation(context, mAnimations[msg.what % 2]);
            anim.setFillAfter(true);
            anim.setAnimationListener(new Animation.AnimationListener() {
                public void onAnimationStart(Animation animation) {
                }

                public void onAnimationRepeat(Animation animation) {
                }

                public void onAnimationEnd(Animation animation) {
                    // TODO Auto-generated method stub
                    handler.sendEmptyMessage(index++);
                }
            });
            view.setAnimation(anim);
        }
    };
}
